package com.mytoutou.playermanager.slice;

import com.mytoutou.playermanager.ResourceTable;
import com.mytoutou.playermanager.player_manager.manager.PlayerItemChangeListener;
import com.mytoutou.playermanager.player_manager.manager.SingleVideoPlayerManager;
import com.mytoutou.playermanager.player_manager.meta.MetaData;
import com.mytoutou.playermanager.player_manager.player_messages.PlayerMessage;
import com.mytoutou.playermanager.player_manager.ui.MediaPlayerWrapper;
import com.mytoutou.playermanager.player_manager.ui.SimpleMainThreadMediaPlayerListener;
import com.mytoutou.playermanager.player_manager.ui.VideoPlayerView;
import com.mytoutou.playermanager.util.Mp4DownloadUtils;
import com.mytoutou.playermanager.util.PlayInfo;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Slider;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.io.File;
import java.util.Map;

public class MainAbilitySlice extends AbilitySlice {
    private SingleVideoPlayerManager playerManager;
    private MEventHandler mEventHandler;
    private VideoPlayerView view;
    private Slider slider;
    //滑动进度条标记
    private boolean flag = false;
    //当前的进度
    private long current;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        mEventHandler = new MEventHandler(EventRunner.getMainEventRunner());
        super.setUIContent(ResourceTable.Layout_ability_main);
        //初始化播放器管理
        playerManager = new SingleVideoPlayerManager(new PlayerItemChangeListener() {
            @Override
            public void onPlayerItemChanged(MetaData currentItemMetaData) {

            }
        });
        //构建播放页面
        view = (VideoPlayerView) findComponentById(ResourceTable.Id_video);
        //防止画面没显示出来
        view.pinToZTop(false);
        //重复播放

        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        String url = "http://vfx.mtime.cn/Video/2019/03/19/mp4/190319222227698228.mp4";
        //通过这个工具通知播放器可以播放了
        File file = new File(getContext().getExternalCacheDir().getPath() + "/" + "fileName.mp4");
        if (file.exists()) {
            file.delete();
        }
        Mp4DownloadUtils.downloadMp4File(url, "fileName.mp4", mEventHandler, getContext(), view);
        //滑动控制进度条
        slider = (Slider) findComponentById(ResourceTable.Id_slider);
        slider.setVisibility(Component.INVISIBLE);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progressValue, boolean b) {
                current = (long) (view.getDuration() * ((float) progressValue / 100)) * 1000;
                System.out.println("当前进度：" + current);
                if (flag) {
                    view.rewindTo(current);
                }
            }

            @Override
            public void onTouchStart(Slider slider) {
                flag = true;
            }

            @Override
            public void onTouchEnd(Slider slider) {
//                view.start();]
                System.out.println("当前状态：" + view.getCurrentState());
                if (view.getCurrentState() == MediaPlayerWrapper.State.PLAYBACK_COMPLETED) {
                    view.start();
                }
                flag = false;
            }
        });
        //播放器主线程
        view.addMediaPlayerListener(new MyMainThreadMediaPlayerListener(mEventHandler, view));
    }


    private class MEventHandler extends EventHandler {

        public MEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int eventId = event.eventId;
            switch (eventId) {
                case Mp4DownloadUtils.PLAYER_MP4_MSG:
                    System.out.println("通知播放");
                    Object object = event.object;
                    PlayInfo playInfo = (PlayInfo) object;
                    String playURL = playInfo.getUrl();
                    VideoPlayerView view = playInfo.getView();
//                    view.setVideoHeight(1200);//设置高度
//                    view.setVideoWidth(500);//设置宽度
                    view.setMarginTop(200);
                    slider.setVisibility(Component.VISIBLE);
                    playerManager.playNewVideo(null, view, playURL);
                    break;
                case Mp4DownloadUtils.DOWNLOAD_MP4_LOADING_PROCESS:
                    System.out.println("下载进度：" + event.object);
                    break;
                case MyMainThreadMediaPlayerListener.PRESS_VALUE:
                    System.out.println("播放进度：" + event.object);
                    String s = (String) event.object;
                    if (!flag) {
                        slider.setProgressValue(Integer.parseInt(s));
                    }
                    break;
                default:
                    System.out.println("不支持渲染的类型");
            }
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
